# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Compute the accuarcy from output bin files."""

import os
import numpy as np


OUTPUT = "./binoutput/"   # 最后要加 "/"
output_num = 0
check_num = 0

files = [f for f in os.listdir(OUTPUT) if f.endswith('.bin')]
for f in files:
    output_num += 1
    tmp = np.fromfile(OUTPUT + f, dtype='float32')
    inf_label = int(np.argmax(tmp))
    label = int(f[:3])

    print("%s, inference label:%d, gt_label:%d" % (f, inf_label, label))
    if inf_label == label:
        check_num += 1


top1_accuarcy = check_num / output_num
print("Totol pic num: %d, Top1 accuarcy: %.4f" % (output_num, top1_accuarcy))
